package com.itheima.web;
/**
 * Create By Awen on 2021/8/25
 *
 * @author Awen
 */

import com.itheima.pojo.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.获取请求标记
            String methodName = request.getParameter("method");
            System.out.println("请求的方法名为: "+methodName);
            //2.获取执行者类的字节码对象UserServlet
            System.out.println("this = "+this);
            Class clazz = this.getClass();
            System.out.println("clazz = " + clazz);
            //3.获取方法的字节码对象
            Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            System.out.println("method = " + method);
            //4.反射执行方法
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("反射异常");
            Result result = new Result(false,"请求功能不存在",null);
            BaseController.responseJson(response,result);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
